<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");

$code = checkTrim($_POST['code']);
$name = checkTrim($_POST['name']);
$url = checkTrim($_POST['url']);
$price = checkTrim($_POST['price']);
$version = checkTrim($_POST['version']);
$remark = checkTrim($_POST['remark']);
$validateDays = checkTrim($_POST['validateDays']);
$isModify = $_POST['isModify'];


$db = new DBC();
$is_code_exist = false;
if ($isModify == 1) {
    $sql = "UPDATE t_hibaby_gold SET name = '" . $name . "',remark = '" . $remark . "',url = '" . $url . "',price = '" . $price . "',version = '" . $version . "',validateDays = " . $validateDays . " WHERE code = '" . $code . "'";
} else {
    $sql = "SELECT code FROM t_hibaby_gold WHERE code= '" . $code . "'";
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exist = true;
    }
    $sql = "INSERT INTO t_hibaby_gold(code,name,remark,price,url,version,validateDays,createTime) VALUES('" . $code . "','" . $name . "','" . $remark . "'," . $price . ",'" . $url . "','" . $version . "','" . $validateDays . "',now())";
}

if ($is_code_exist) {
    $msg = "编号 " . $code . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    $transfer = "goldList.php?menu=gold";
    if ($db->execute($sql)) {
        //success
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
